package com.sheng.center.action;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sheng.center.entity.Student;
import com.sheng.center.entity.User;
import com.sheng.center.service.StudentService;
import com.sheng.center.service.UserService;
import com.sheng.center.util.MyTime;

public class StudentAction extends ActionSupport implements ModelDriven<Student>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 211698107579903582L;
	protected String forwardView;
	protected static final String ADD_JSP = "/WEB-INF/page/Student_add.jsp";
	protected static final String EDIT_JSP = "/WEB-INF/page/Student_edit.jsp";
	protected static final String LIST_JSP = "/WEB-INF/page/Student_list.jsp";
    private Student student = new Student();
    private List<Student> list;
    private File file;
    private String fileFileName;
    private User user = new User();
	private UserService userService;
	private StudentService studentService;
	@Override
	public Student getModel() {
		return student;
	}
	
	
	public String openAdd(){
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	public String openEdit() {
		student = studentService.findById(student);
		setForwardView(EDIT_JSP);
		return SUCCESS;
	}
	
	public String list() {
		list = studentService.findAllList(student);
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Title: add
	 * @Description: 图片保存方法
	 * @param @return
	 * @param @throws IOException 参数throws IOException表示此方法有抛出IOException异常的可能性。
		只要此方法的某些代码可能抛出异常，你就必须显式的处理他，要么抓住(try...catch)，要么抛出。
		抛出i/o异常!因为应用了I/O流，所以有抛出IOException异常的可能性,就应该这样写，这是java的健壮性的体现!!
	 * @return String 返回类型
	 * @author huangshengjun 1532950421@qq.com
	 * @date 2019年12月14日 下午12:34:18 
	 * @throws
	 */
	public String add()throws IOException {
		System.out.println("addAction==="+student.getStudentId()+";"+student.getUsername()+";"+student.getPassword()+";"+student.getMajores()
		+";"+student.getCollege()+";"+student.getStudentClass()+";"+student.getTel()+";"+student.getTel()+";");
		System.out.println("addAction==来到了学生添加,file==="+file+";saveAddress==="+student.getSaveAddress());
		if (file != null) {
			System.out.println("addAction==来到了图片上传,file==="+file);
			String savePath = "/upload/studentImages";// 保存地址
			System.out.println("来到了图片上传保存地址==="+savePath);
//			String saveAddress="<%=basePath%>/upload/dataset/images";//数据库存储地址
			// 图片编号命名格式为：时间戳+1位随机数+文件名
			student.setImageId(MyTime.getNo(2) + fileFileName.substring(fileFileName.lastIndexOf(".")));
			student.setSaveAddress(student.getSaveAddress() + savePath + "/" + student.getImageId());
			System.out.println("图片===="+student.getSaveAddress());
			// 获得上传图片的服务器端路径.
			String path = ServletActionContext.getServletContext().getRealPath(savePath);
			System.out.println("上传图片的服务器端路径==="+path);
			// 创建文件类型对象:
			File diskFile = new File(path + "//" + student.getImageId());
			FileUtils.copyFile(file, diskFile);// 文件上传:
			System.out.println("addAction===图片上传完成");
		}
		//把学生的账号密码写入user表
		user.setUsername(student.getUsername());
		user.setPassword(student.getPassword());
		user.setIsUser(1);//学生标识
		userService.register(user);
		studentService.add(student);
		setForwardView(LIST_JSP);
		return list();
	}
	
	public String edit() {
		Student student1 = studentService.findById(student);
		student.setSaveAddress(student1.getSaveAddress());
		student.setUsername(student1.getUsername());
		student.setImageId(student1.getImageId());
		studentService.edit(student);
		return list();
	}
	
	public String delete() {
		student = studentService.findById(student);
		studentService.delete(student);
		return list();
	}


	public String getForwardView() {
		return forwardView;
	}


	public void setForwardView(String forwardView) {
		this.forwardView = forwardView;
	}


	public Student getStudent() {
		return student;
	}


	public void setStudent(Student student) {
		this.student = student;
	}


	public List<Student> getList() {
		return list;
	}


	public void setList(List<Student> list) {
		this.list = list;
	}


	public StudentService getStudentService() {
		return studentService;
	}


	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}


	public File getFile() {
		return file;
	}


	public void setFile(File file) {
		this.file = file;
	}


	public String getFileFileName() {
		return fileFileName;
	}


	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}


	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
